﻿using UnityEngine;
using System.Collections;

[USequencerEvent("物理/加力")]
public class USApplyForceEvent : USEventBase {
	public Vector3 direction = Vector3.up;
	
	public float strength = 1.0f;
	public ForceMode type = ForceMode.Impulse;
	
	private Transform previousTransform = null;
	
	public override void FireEvent()
	{	
		Rigidbody affectedBody = AffectedObject.GetComponent<Rigidbody>();
		if(!affectedBody)
		{
            Debug.Log("AffectedObject缺少Rigidbody组件");
			return;
		}
		
		affectedBody.AddForceAtPosition(direction * strength, transform.position, type);
		previousTransform = affectedBody.transform;
	}
	
	public override void ProcessEvent(float deltaTime)
	{
		
	}
	
	public override void StopEvent()
	{
		UndoEvent();
	}
	
	public override void UndoEvent()
	{
		Rigidbody affectedBody = AffectedObject.GetComponent<Rigidbody>();
		if(!affectedBody)
			return;
		
		affectedBody.Sleep();
		if(previousTransform)
		{
			AffectedObject.transform.position = previousTransform.position;
			AffectedObject.transform.rotation = previousTransform.rotation;
		}
	}
}
